//
//  HTTruckLengthTypeVC.m
//  TruckDriver
//
//  Created by Ting Han on 2017/10/19.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTTruckLengthTypeVC.h"
#import "HTTruckLengthTypeCell.h"
#import "HTTruckLengthTypeHeader.h"

@interface HTTruckLengthTypeVC ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;

@property (nonatomic,strong) NSDictionary *plistData;
@property (nonatomic,strong) NSMutableArray *lengthsData;
@property (nonatomic,strong) NSMutableArray *truckTypesData;

@property (nonatomic,strong) HTTruckTypeModel *selectedLength;
@property (nonatomic,strong) HTTruckTypeModel *selectedTruckType;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *safeBottomHeight;

@end

@implementation HTTruckLengthTypeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"车长车型";
    [self setupSubs];
    [self loadInitData];
}
- (void)setupSubs{
    self.safeBottomHeight.constant = IPHONE_SAFEBOTTOMAREA_HEIGHT;
    [self.flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    CGFloat width = (kHTScreenWidth - 50)/4.0;
    self.flowLayout.itemSize = CGSizeMake(width, kWidth(30));
    self.flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 5, 10);
    self.flowLayout.minimumLineSpacing = 15;
    self.flowLayout.minimumInteritemSpacing = 0;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HTTruckLengthTypeCell" bundle:nil] forCellWithReuseIdentifier:TruckLengthTypeCell];
    [self.collectionView registerNib:[UINib nibWithNibName:@"HTTruckLengthTypeHeader" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:TruckLengthTypeHeader];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;

    
}
- (NSMutableArray *)lengthsData{
    if (!_lengthsData) {
        _lengthsData = [NSMutableArray array];
    }
    return _lengthsData;
}
- (NSMutableArray *)truckTypesData{
    if (!_truckTypesData) {
        _truckTypesData = [NSMutableArray array];
    }
    return _truckTypesData;
}
- (void)loadInitData{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Trucks" ofType:@"plist"];
    self.plistData =[NSDictionary dictionaryWithContentsOfFile:path];
   
    NSArray *lengths =  self.plistData[@"TruckLength"];
    NSArray *truckType =  self.plistData[@"TruckType"];
    [self.lengthsData removeAllObjects];
    [self.truckTypesData removeAllObjects];
    NSInteger index = 0;
    [self.lengthsData addObject:[HTTruckTypeModel truckTypeModelWithTitle:@"不限" isSelected:YES TruckTypeModelType:TruckTypeModelTypeUnlimited index:index]];
    index ++;
    for (int i = 0; i < lengths.count; i++) {
        [self.lengthsData addObject:[HTTruckTypeModel truckTypeModelWithTitle:lengths[i] isSelected:NO TruckTypeModelType:TruckTypeModelTypeDefault index:index]];
        index++;
    }
    if (!([HTMUserDefaultUtil getHistoryLength] == nil || [HTMUserDefaultUtil getHistoryLength].length <= 0)) {
        [self.lengthsData addObject:[HTTruckTypeModel truckTypeModelWithTitle:[HTMUserDefaultUtil getHistoryLength] isSelected:NO TruckTypeModelType:TruckTypeModelTypeDefault index:index]];
    }
    index = 0;
    [self.truckTypesData addObject:[HTTruckTypeModel truckTypeModelWithTitle:@"不限" isSelected:YES TruckTypeModelType:TruckTypeModelTypeUnlimited index:index]];
    index ++;
    for (int i = 0; i < truckType.count; i++) {
        [self.truckTypesData addObject:[HTTruckTypeModel truckTypeModelWithTitle:truckType[i] isSelected:NO TruckTypeModelType:TruckTypeModelTypeDefault index:index]];
        index++;
    }
    self.selectedLength = self.lengthsData.firstObject;
    self.selectedTruckType = self.truckTypesData.firstObject;
   
}
- (IBAction)didClickCommit:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(didChoiceLength:type:)]) {
        [self.delegate didChoiceLength:self.selectedLength.title type:self.selectedTruckType.title];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    switch (section) {
        case 0://车长
        {
            return  self.lengthsData.count;
        }
            break;
        case 1://车型
        {
            return self.truckTypesData.count;
        }
            break;
        default: return 0;
            break;
    }
    return 10;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(kHTScreenWidth, 60);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HTTruckLengthTypeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:TruckLengthTypeCell forIndexPath:indexPath];
    switch (indexPath.section) {
        case 0:
        {
            cell.truckModel = self.lengthsData[indexPath.row];
        }
            break;
        case 1:
        {
            cell.truckModel = self.truckTypesData[indexPath.row];
        }
            break;
        default:
            break;
    }
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    UICollectionReusableView *reusableView = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        HTTruckLengthTypeHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:TruckLengthTypeHeader forIndexPath:indexPath];
        if (indexPath.section == 0) {
             header.titleLabel.text = @"车长：";
        }else{
             header.titleLabel.text = @"车型：";
        }
        reusableView = header;
    }
    return reusableView;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HTTruckLengthTypeCell *cell =(HTTruckLengthTypeCell *) [collectionView cellForItemAtIndexPath:indexPath];
    if (indexPath.section == 0) {//车长
        cell.isChosed = !cell.isChosed;
        if (cell.isChosed) {
            for (HTTruckTypeModel *model in self.lengthsData) {
                if ([model.title isEqualToString:self.selectedLength.title]) {
                    model.isSelected = NO;
                    break;
                }
            }
            self.selectedLength = cell.truckModel;
        }else{
            HTTruckTypeModel *model = self.lengthsData.firstObject;
            model.isSelected = YES;
            self.selectedLength = model;
        }
         [collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
    }else{//车型
        cell.isChosed = !cell.isChosed;
        if (cell.isChosed) {
            for (HTTruckTypeModel *model in self.truckTypesData) {
                if ([model.title isEqualToString:self.selectedTruckType.title]) {
                    model.isSelected = NO;
                    break;
                }
            }
            self.selectedTruckType = cell.truckModel;
        }else{
            HTTruckTypeModel *model = self.truckTypesData.firstObject;
            model.isSelected = YES;
            self.selectedTruckType = model;
        }
         [collectionView reloadSections:[NSIndexSet indexSetWithIndex:1]];
    }
}
@end
